/**
 * 
 */
package org.swing.utility.jai.component;

import java.awt.BorderLayout;
import java.awt.image.RenderedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

/**
 * @author lqnhu
 *
 */
public class RenderedImageBrowser extends JPanel {
	ImageTreeModel model;
	JTree imageTree;
	RenderedImageInfoPanel imageInfo;
	JSplitPane split;
	boolean showHistogram;
	boolean showRoi;

	public static void showChain(RenderedImage image) {
		showChain(image, true, false);
	}

	public static void showChain(RenderedImage image,
			final boolean showHistogram) {
		showChain(image, showHistogram, false);
	}

	public static void showChain(RenderedImage image,
			final boolean showHistogram, final boolean showRoi) {
		JFrame frame = new JFrame("Rendered image information tool");
		RenderedImageBrowser info = new RenderedImageBrowser(showHistogram,
				showRoi);
		info.setImage(image);
		frame.setContentPane(info);
		frame.setSize(1024, 768);
		frame.setVisible(true);
	}

	public RenderedImageBrowser() {
		this(true, false);
	}

	public RenderedImageBrowser(final boolean showHistogram,
			final boolean showRoi) {
		this.showHistogram = showHistogram;
		this.showRoi = showRoi;
		model = new ImageTreeModel();
		imageTree = new JTree(model);
		imageTree.setCellRenderer(new ImageTreeRenderer());
		imageTree.setShowsRootHandles(true);
		imageTree.putClientProperty("JTree.lineStyle", "Angled");
		imageInfo = new RenderedImageInfoPanel(showHistogram, showRoi);
		split = new JSplitPane();
		split.setLeftComponent(new JScrollPane(imageTree));
		split.setRightComponent(imageInfo);
		split.setResizeWeight(0.2);
		setLayout(new BorderLayout());
		add(split);
		imageTree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				final TreePath selectedpath = imageTree.getSelectionPath();
				if (selectedpath == null)
					imageTree.setSelectionRow(0);
				RenderedImage image = (RenderedImage) imageTree
						.getSelectionPath().getLastPathComponent();
				imageInfo.setImage(image);
			}
		});
	}

	public void setImage(RenderedImage image) {
		if (image == null)
			return;
		model.setRoot(image);
		imageTree.setSelectionPath(new TreePath(image));
		int rc = 0;
		do {
			rc = imageTree.getRowCount();
			for (int x = rc; x >= 0; x--) {
				imageTree.expandRow(x);
			}
		} while (rc != imageTree.getRowCount());
	}
}
